﻿#include "cat-app.hpp"

#include "cat-bake-end.hpp"
#include "cat-bake-end.cpp"

#include "cat-server.hpp"

#include <thread>

using namespace CatBakeEnd;

namespace {
	//渲染线程
	static void RenderProcess(void (*CallBack)(),char* config) {
		CATSERVER::CatServer server;
		srand((unsigned)time(NULL));
		int port = 600000 + rand() % 100000;

		if (!BakeEndInit(config))
			goto end;	

		server.ReceivedDataEvt = DataExchange;

		server.StartServer(port);

		ShellExecute((HWND)cat_get_handle(), 0, L"UI.exe", std::to_wstring(port).c_str(), 0, SW_SHOWNORMAL);

		while (BakeEndRun());

	end:
		server.StopServer();
		BakeEndRelease();

		if (CallBack)
			CallBack();
	}

	static bool isRun = true;
	void RenderEndCallBack() {
		isRun = false; 
		Sleep(1000);
	}
}

CATApp::CATApp(int argc, char* argv[]):
	defaultConfig(NULL)
{
	if (argc > 0)
	{
		defaultConfig = argv[0];
	}
}

CATApp::~CATApp()
{
}

bool CATApp::APPInit()
{
	return true;
}

int CATApp::Run()
{	
	std::thread rp(RenderProcess, RenderEndCallBack,defaultConfig);
	
	while (isRun) {
		Sleep(1000);
	}
	rp.join();	

	return 0;
}

void CATApp::APPRelease()
{
	
	
}
